package com.project.config;

import com.project.utils.ShiroUtils;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.ArrayList;


/**
 * @ProjectName:
 * @Package:
 * @ClassName:
 * @Author: ZhaoLongShi
 * @CreateDate:
 */
@SuppressWarnings("all")
@Configuration
public class ShiroConfig {
    @Bean
    public UserRealm userRealm() {
        return new UserRealm();
    }

    @Bean
    public AdminRealm adminRealm(){return new AdminRealm();}


    /**
     * 路径过滤器
     *
     * @return
     */
    @Bean
    public ShiroFilterChainDefinition filterChainDefinition() {
        DefaultShiroFilterChainDefinition defaultShiroFilterChainDefinition = new DefaultShiroFilterChainDefinition();
        //不需要认证的请求
        defaultShiroFilterChainDefinition.addPathDefinition("/user/login", "anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/admin/login", "anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/user/register", "anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/test", "anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/table/number/**", "anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/table/reserve/**", "anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/order/**", "anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/user/login/phone", "anon");
        defaultShiroFilterChainDefinition.addPathDefinition("/user//msg", "anon");
        //其他所有都需要认证
        defaultShiroFilterChainDefinition.addPathDefinition("/**", "authc");
        return defaultShiroFilterChainDefinition;
    }

    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(UserRealm userRealm,
                                                               AdminRealm adminRealm,
                                                               CustomSessionManager webSessionManager) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        /**
         * 设置自定义realm对象
         */
        ArrayList realms = new ArrayList<>();
        realms.add(userRealm);
        realms.add(adminRealm);
        defaultWebSecurityManager.setRealms(realms);
        defaultWebSecurityManager.setSessionManager(webSessionManager);
        return defaultWebSecurityManager;
    }

    /**
     * 配置密码加密算法，注意一定要跟注册时的保持一致
     * md5
     * sha256
     * sha512
     *
     * @return
     */
    @Bean
    @Primary
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        //创建加密算法对象，并指定加密方式 Md5
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(Md5Hash.ALGORITHM_NAME);
        //设置加密次数
        hashedCredentialsMatcher.setHashIterations(ShiroUtils.HASH_ITERATIONS);
        hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
        return hashedCredentialsMatcher;
    }

    /**
     * 请求头中
     *
     * @return
     */
    @Bean
    public CustomSessionManager webSessionManager() {
        CustomSessionManager customSessionManager = new CustomSessionManager();
        return customSessionManager;
    }
}
